package com.gxp.controller.user;


import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gxp.pojo.User;
import com.gxp.service.UserService;
import com.gxp.util.security.Des;
import com.gxp.validator.UserValidator;

@Controller
public class PublicUserController {
	
	Logger logger = Logger.getLogger(PublicUserController.class);

	private String userId;
	
	@Autowired
	private UserService userService;
	
	@InitBinder
	protected void initBinder(WebDataBinder binder){
		binder.setValidator(new UserValidator());
	}
	
	@ModelAttribute
    public void getUserId() {
		UserDetails userDetail = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		userId = userDetail.getUsername();
		logger.debug("===========================curent user is : " + userId);
    }
	
	@RequestMapping(value="/public/user/editUserPassword",method=RequestMethod.GET)
	public ModelAndView editUserPassword() {	
		ModelAndView mav = new ModelAndView();
		
		User user = userService.getActiveUser(userId);
		mav.addObject("user",user);
		mav.setViewName("public/user/editUserPassword");
		
		return mav;
	}
	
	@RequestMapping(value="/public/user/editUserInfo",method=RequestMethod.GET)
	public ModelAndView editUserInfo() {	
		ModelAndView mav = new ModelAndView();
		
		User userReturn = userService.getActiveUser(userId);
		System.out.println("*********>get user info for UserId:"+userId);
		System.out.println("*********>user id:"+userReturn.getUserId());
		System.out.println("*********>user name:"+userReturn.getUserM());
		
		mav.addObject("user",userReturn);
		mav.setViewName("public/user/editUserInfo");
		
		return mav;		
	}
	
	@RequestMapping(value="/public/user/updateUserPassword",method=RequestMethod.POST)
	@ResponseBody
	public String updateUserPassword(@Valid User user, BindingResult result) {
		
		if (!StringUtils.hasText(user.getPassword())) {
			return "failed";
		}
		String userId = user.getUserId();
		String password = user.getPassword();
		String encryptPassword = Des.encryptUserPassord(userId, password);
		user.setPassword(encryptPassword);
		userService.updateUserPassword(user);
		
		return "success";
	}
	
	@RequestMapping(value="/public/user/update",method=RequestMethod.POST)
	@ResponseBody
	public String updateUserInfo(User user) {
		
		if (!StringUtils.hasText(user.getUserM())) {
			return "failed";
		}
		userService.updateUserInfo(user);
		
		return "success";
	} 
	
	@RequestMapping(value="/public/user/checkpassword",method=RequestMethod.POST)
	@ResponseBody
	public boolean checkPassword(User user) {
		return userService.checkPassword(user);
	}

}
